<?php

/**
 * 后台权限相关的方法封装
 *
 * @author suing <snsnsky@gmail.com>
 */

namespace Admin;

use \Common\Bridge;

class AdminAuth
{
    /**
     * 获得所有管理员分组
     *
     * @return array
     */
    public static function getAllGroup()
    {
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);

        $data = $dbr->query("select * from AdminGroup")->fetchAll();
        return $data;
    }

    /**
     * 根据ID获取分组
     *
     * @param int $id   分组ID
     * @return array
     */
    public static function getGroupById(int $id)
    {
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);

        $data = $dbr->query("select * from AdminGroup where AdminGroupId={$id}")->fetch();
        return $data;
    }

    /**
     * 获取管理员列表，并将用户所在权限组信息放在 GroupData 数组中一起返回
     *
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public static function getAdminUser(int $offset = 0, int $limit = 10)
    {
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);

        $data = $dbr->query("select * from AdminUser limit {$limit} offset {$offset}")->fetchAll();
        empty($data) && $data = [];
        foreach ($data as &$v)
        {
            $group = self::getGroupById($v['AdminUserGroup']);
            $v['GroupData'] = $group;
        }
        return $data;
    }

    /**
     * 根据user id获取管理员信息
     *
     * @param int $id    用户ID
     * @return mixed
     */
    public static function getAdminUserById(int $id)
    {
        $dbr = Bridge::pdo(Bridge::DB_ADMIN_R);

        $data = $dbr->query("select * from AdminUser where AdminUserId={$id}")->fetch();
        if (!empty($data))
        {
            $group = self::getGroupById($data['AdminUserGroup']);
            $data['GroupData'] = $group;
        }
        return $data;
    }

}